package ecstore.client.customWidgets;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;


import ecstore.client.dataOperation.ItemInfo;

public class ItemEditorPanel extends Composite {
	VerticalPanel verticalPanel = new VerticalPanel();
//	private final DataStorageServiceAsync dataStorageService = GWT.create(DataStorageService.class);
	private String itemCategory = "Tobacco";
	
	final FlexTable flexTable = new FlexTable();
	final ListBox categoryListBox = new ListBox();
	final TextBox brandBox = new TextBox();
	final TextBox itemNameBox = new TextBox();
	final TextBox itemPriceBox = new TextBox();
	final TextBox itemSizeBox = new TextBox();
	final DateBox addedDateBox = new DateBox();
	final TextBox thumbnailBox = new TextBox();
	final TextBox imageBox = new TextBox();
	final TextBox stockBox = new TextBox();
	final TextBox recmdStockBox = new TextBox();
	final TextArea itemDescriptionBox = new TextArea();
	
	final Label lblWindowNumber = new Label("Window number:");
	final TextBox windowNumBox = new TextBox();
	final Label lblRow_1 = new Label("Row:");
	final TextBox rowBox = new TextBox();
	final Label lblRow = new Label("Col:");
	final TextBox colBox = new TextBox();

	
	public ItemEditorPanel() {
		verticalPanel.setSize("628px", "414px");
		
		
		initWidget(verticalPanel);
		
		
		verticalPanel.add(flexTable);
		flexTable.setSize("423px", "385px");
		
		Label lblCategory = new Label("Category:");
		flexTable.setWidget(0, 0, lblCategory);
		flexTable.setWidget(0, 1, categoryListBox);
		categoryListBox.setVisibleItemCount(1);
		categoryListBox.addItem("Tobacco");
		categoryListBox.addItem("Cigarettes");
		categoryListBox.addItem("Others");
		
		Label lblBrand = new Label("Brand:");
		flexTable.setWidget(1, 0, lblBrand);
		flexTable.setWidget(1, 1, brandBox);
		
		Label lblItemName = new Label("Item Name:");
		flexTable.setWidget(2, 0, lblItemName);
		flexTable.setWidget(2, 1, itemNameBox);
		
		Label lblPrice = new Label("Price:");
		flexTable.setWidget(3, 0, lblPrice);
		flexTable.setWidget(3, 1, itemPriceBox);
		
		Label lblSize = new Label("Size:");
		flexTable.setWidget(4, 0, lblSize);
		flexTable.setWidget(4, 1, itemSizeBox);
		
		Label lblAddedDate = new Label("Added date:");
		flexTable.setWidget(5, 0, lblAddedDate);
		
		
		flexTable.setWidget(5, 1, addedDateBox);
		addedDateBox.setSize("189", "27");
		
		Label lblThumbnail = new Label("Thumbnail:");
		flexTable.setWidget(6, 0, lblThumbnail);
		flexTable.setWidget(6, 1, thumbnailBox);
		
		Label lblImage = new Label("Image:");
		flexTable.setWidget(7, 0, lblImage);
		flexTable.setWidget(7, 1, imageBox);
		
		Label lblStock = new Label("Stock");
		flexTable.setWidget(11, 0, lblStock);
		flexTable.setWidget(11, 1, stockBox);
		
		Label lblRecmdStock = new Label("Recmd Stock");
		flexTable.setWidget(12, 0, lblRecmdStock);
		flexTable.setWidget(12, 1, recmdStockBox);
		
		Label lblDescription = new Label("Description:");
		flexTable.setWidget(13, 0, lblDescription);
		flexTable.setWidget(13, 1, itemDescriptionBox);
				
		flexTable.setWidget(8, 0, lblWindowNumber);
		flexTable.setWidget(8, 1, windowNumBox);

		flexTable.setWidget(9, 0, lblRow_1);
		flexTable.setWidget(9, 1, rowBox);
		

		flexTable.setWidget(10, 0, lblRow);
		flexTable.setWidget(10, 1, colBox);
		// Change the panel when there is no item position needed
		categoryListBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				itemCategory = categoryListBox.getItemText(categoryListBox.getSelectedIndex());
				if(itemCategory.equalsIgnoreCase("Tobacco") || itemCategory.equalsIgnoreCase("Cigarettes")){

					flexTable.setWidget(8, 0, lblWindowNumber);
					flexTable.setWidget(8, 1, windowNumBox);

					flexTable.setWidget(9, 0, lblRow_1);
					flexTable.setWidget(9, 1, rowBox);
					

					flexTable.setWidget(10, 0, lblRow);
					flexTable.setWidget(10, 1, colBox);
					
				} else{
					flexTable.clearCell(8, 0);
					flexTable.clearCell(8, 1);
					flexTable.clearCell(9, 0);
					flexTable.clearCell(9, 1);
					flexTable.clearCell(10, 0);
					flexTable.clearCell(10, 1);
				}
				
			}
		});
		
	}
	
	/**
	 * Retrieve the item infomation according to the current item box content.
	 * @return
	 */
	public ItemInfo getItemInfo(){
		ItemInfo itemInfo;
		String category = categoryListBox.getItemText(categoryListBox.getSelectedIndex());
		String brand = brandBox.getText();
		String name = itemNameBox.getText();
		float price = Float.valueOf(itemPriceBox.getText()) ;
		
		String size = itemSizeBox.getText();
		String description = itemDescriptionBox.getText();
		if(description.equalsIgnoreCase("")){
			System.out.println("not null");
			System.out.println(description);
		}else{
			System.out.println("null description");
		}
		String thumbnail = thumbnailBox.getText();
		String image = imageBox.getText();
		
		int stock = Integer.valueOf(stockBox.getText());
		int recmdStock = Integer.valueOf(recmdStockBox.getText());

		if(itemCategory.equalsIgnoreCase("Tobacco") || itemCategory.equalsIgnoreCase("Cigarettes")){
			int windowNum = Integer.valueOf(windowNumBox.getText());
			int row = Integer.valueOf(rowBox.getText());
			int col = Integer.valueOf(colBox.getText());
			
			itemInfo = new ItemInfo(category, brand, name, price, size, description,
							thumbnail, image, windowNum, row, col, stock, recmdStock);
		}else{
			itemInfo = new ItemInfo(category, brand, name, price, size, description,
							thumbnail, image, stock, recmdStock);
		}
		
		return itemInfo;
	}
	
	/**
	 * Set the corresponding item box according to the given item
	 * @param itemInfo
	 */
	public void setPanel(ItemInfo itemInfo){
		//TODO set the category selected item
//		categoryListBox.setSelectedIndex(categoryListBox.)
		brandBox.setText(itemInfo.getBrand());
		itemNameBox.setText(itemInfo.getName());
		itemPriceBox.setText(String.valueOf(itemInfo.getPrice()));
		itemSizeBox.setText(itemInfo.getSize());
		//TODO set the added date
//		addedDateBox.setValue(itemInfo.get)
		thumbnailBox.setText(itemInfo.getThumbnail());
		imageBox.setText(itemInfo.getImage());
		
		if(itemCategory.equalsIgnoreCase("Tobacco") || itemCategory.equalsIgnoreCase("Cigarettes")){
			windowNumBox.setText(String.valueOf(itemInfo.getWindowNum()));
			rowBox.setText(String.valueOf(itemInfo.getRowNum()));
			colBox.setText(String.valueOf(itemInfo.getColNum()));
		}
		
		stockBox.setText(String.valueOf(itemInfo.getStock()));
		recmdStockBox.setText(String.valueOf(itemInfo.getRecmdStock()));
		itemDescriptionBox.setText(itemInfo.getDescription());
	}
	
	//TODO set category and date
	public void setBrand(String newBrand){
		brandBox.setText(newBrand);
	}

	public void setName(String newName){
		itemNameBox.setText(newName);
	}
	
	public void setItemSize(String newSize){
		itemSizeBox.setText(newSize);
	}
	
	public void setPrice(float newPrice){
		itemPriceBox.setText(String.valueOf(newPrice));
	}
	
	public void setThumbnail(String thumbnailURL){
		thumbnailBox.setText(thumbnailURL);
	}
	
	public void setImage(String imageURL){
		imageBox.setText(imageURL);
	}
	
	public void setWindowNumber(int newWindowNum){
		windowNumBox.setText(String.valueOf(newWindowNum));
	}
	
	public void setRow(int newRow){
		rowBox.setText(String.valueOf(newRow));
	}
	
	public void setCol(int newCol){
		colBox.setText(String.valueOf(newCol));
	}
	
	public void setDescription(String newDescription){
		itemDescriptionBox.setText(newDescription);
	}
}
